package com.adobe.training.core;

import java.io.IOException;


import javax.servlet.ServletException;

import javax.jcr.Repository;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;



import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
//test

    @SlingServlet(paths = "/bin/company/repo", methods = "GET") 
    public class MySafeMethodServlet extends SlingSafeMethodsServlet {


    @Reference
    private Repository repository;
    private static final long serialVersionUID =-3960692666512058118L;
    
    
    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Content-Type", "application/json");
        response.getWriter().print("{\"coming\" : \"soon\"}");
        String[] keys = repository.getDescriptorKeys(); 
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i< keys.length; i++) {
            try{
                jsonObject.put(keys[i], repository.getDescriptor(keys[i]));
            } 
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
        response.getWriter().print(jsonObject.toString());
    }
}